import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import users

import base
import data
import msg

from base_page import BaseHandler

class ProfileHandler(BaseHandler):
  def GetRoleTypes(self, user):
    role_types = []
    pending_types = []
    # get pending role types first
    q = data.UserLevelApplication.all().filter('creator =', user)
    results = q.filter('state =', data.UserLevelApplication.PENDING)
    for app in results:
      pending_types.append(app.type)

    roles = [
      (user.is_sys_admin, data.UserLevelApplication.SYS_ADMIN,
       msg.Get(data.GetUserLevelApplicationTypeStr(data.UserLevelApplication.SYS_ADMIN))),
      (user.is_request_mod, data.UserLevelApplication.REQUEST_MOD,
       msg.Get(data.GetUserLevelApplicationTypeStr(data.UserLevelApplication.REQUEST_MOD))),
      (user.is_donor_mod, data.UserLevelApplication.DONOR_MOD,
       msg.Get(data.GetUserLevelApplicationTypeStr(data.UserLevelApplication.DONOR_MOD))),
      (user.is_manufacturer_mod, data.UserLevelApplication.MANUFACTURER_MOD,
       msg.Get(data.GetUserLevelApplicationTypeStr(data.UserLevelApplication.MANUFACTURER_MOD))),
    ]
    for is_role, role, name in roles:
      if is_role:
        state = 'current'
      elif role in pending_types:
        state = 'pending'
      else:
        state = 'new'
      new_role = {'name': name, 'type': role, 'state': state}
      role_types.append(new_role)
    return role_types

  def ProcessGet(self):
    self.Setup('profile.html', required_registered=False)
    user = self.GetEntityByKeyName("id")
    if self.user:
      if not user or user.key() == self.user.key():
        user = self.user 
        is_admin = user.is_sys_admin or user.is_request_mod or \
    	           user.is_donor_mod or user.is_manufacturer_mod
        self.template_values['is_self'] = True
        self.template_values['is_admin_profile'] = is_admin

    if not user:
      self.RedirectAndRaise("/")

    self.template_values['nickname'] = user.nickname
    self.template_values['description'] = user.description
    self.template_values['role_types'] =  self.GetRoleTypes(user)
    # Get donor profiles
    q = data.DonorProfile().all().filter('creator =', user).order('-date')
    self.template_values['donor_profiles'] = q  
    # Get manufacturer profiles
    q = data.ManufacturerProfile().all().filter('creator =', user).order('-date')
    self.template_values['manufacturer_profiles'] = q  
    self.Render()

  def IsTypeApplicable(self, user_type):
    if user_type == data.UserLevelApplication.SYS_ADMIN:
      return not self.user.is_sys_admin
    elif user_type == data.UserLevelApplication.REQUEST_MOD:
      return not self.user.is_request_mod
    elif user_type == data.UserLevelApplication.DONOR_MOD:
      return not self.user.is_donor_mod
    elif user_type == data.UserLevelApplication.MANUFACTURER_MOD:
      return not self.user.is_manufacturer_mod
    else:
      return false

  def ProcessPost(self):
    self.Setup('profile.html', required_registered=True)
    success, user_type = base.GetInt(self.request.get('type'))
    if not success: self.RedirectAndRaise("/")
    reason = self.request.get('reason')
    if self.IsTypeApplicable(user_type):
      application = data.UserLevelApplication()
      application.creator = self.user
      application.type = user_type
      application.request_note = reason
      application.state = data.UserLevelApplication.PENDING
      application.put()
    else:
      self.RedirectAndRaise("/")

    self.template_values['nickname'] = self.user.nickname
    self.template_values['description'] = self.user.description
    self.template_values['role_types'] =  self.GetRoleTypes(self.user)
    self.Render()
